package com.mingmen.canting.gongyingshang;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.mingmen.canting.R;
import com.mingmen.canting.base.BaseBean;
import com.mingmen.canting.flycotablelayout.flyco.tablayout.SlidingTabLayout;
import com.mingmen.canting.flycotablelayout.flyco.tablayout.listener.OnTabSelectListener;
import com.mingmen.canting.http.callback.StringCallback;
import com.mingmen.canting.httpconfig.HttpResponse;
import com.mingmen.canting.model.GongYingDetailModel;
import com.mingmen.canting.util.GsonUtil;
import com.mingmen.canting.util.MyStringUtils;
import com.mingmen.canting.util.SharePreferenceUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import okhttp3.Call;
import okhttp3.Response;


public class ShopFragment extends Fragment implements OnTabSelectListener {

    Unbinder unbinder;


    private ArrayList<Fragment> mFragments = new ArrayList<>();
    private MyPagerAdapter mAdapter;
    String token;
    List<GongYingDetailModel.DataBean> data = new ArrayList<>();

    SlidingTabLayout tabLayout_2;


    ViewPager vp;
    String supplierId;

    List<GongYingDetailModel.DataBean> dataBeanList = new ArrayList<>();
    List<String> stringList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_shop, null);
        unbinder = ButterKnife.bind(this, view);
        vp = view.findViewById(R.id.vp);
        tabLayout_2 = view.findViewById(R.id.tl_2);
        token = (String) SharePreferenceUtil.get(getActivity(), "token", "");
        supplierId = (String) SharePreferenceUtil.get(getActivity(), "supplierId", "");
        getData();

//
        Log.d("tokentoken", token);

        tabLayout_2.setOnTabSelectListener(this);
        return view;
    }



    public void getData() {
        HttpResponse.getSupplierCommodity(token, supplierId, new StringCallback(getActivity()) {
            //                    HttpResponse.getSupplierCommodity(token, supplierId, new StringCallback(getActivity()) {
            @Override
            public void onSuccess(String s, Call call, Response response) {

                Log.d("getSupplierCommodity", s);
                BaseBean baseBean = new BaseBean();
                baseBean = GsonUtil.GsonToBean(s, BaseBean.class);
                if (baseBean.getCode() == 0) {
                    GongYingDetailModel shopDetailModel = new GongYingDetailModel();
                    shopDetailModel = GsonUtil.GsonToBean(s, GongYingDetailModel.class);
                    dataBeanList = shopDetailModel.getData();
                    for (int i = 0; i < dataBeanList.size(); i++) {

                        mFragments.add(GongYingNewDetailFragment.getInstance(MyStringUtils.beanToString(dataBeanList.get(i))));
                        stringList.add(dataBeanList.get(i).getCategoryName());
                        Log.d("SimpleCardFragment", dataBeanList.get(i).getCategoryName());
                    }
                    mAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
                    vp.setAdapter(mAdapter);
                    mAdapter.setDataList(stringList);
                    tabLayout_2.setViewPager(vp);
                }

            }
        });
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }


    @Override
    public void onTabSelect(int position) {
//        Toast.makeText(getActivity(), position + "", Toast.LENGTH_LONG).show();
    }

    @Override
    public void onTabReselect(int position) {
//        Toast.makeText(getActivity(), position + "", Toast.LENGTH_LONG).show();
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        List<String> dataList = new ArrayList<>();

        public void setDataList(List<String> dataList) {
            this.dataList = dataList;
            notifyDataSetChanged();
        }

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public int getCount() {
            return mFragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
//            return mTitles[position];
            Log.d("getPageTitle", dataList.get(position));
            return dataList.get(position);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }
    }


    @OnClick({})
    public void onViewClicked(View view) {
        switch (view.getId()) {


        }
    }


}
